﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

public class ViewController : MonoBehaviour
{
    /// <summary>
    /// to identify different interfaces with same class
    /// </summary>
    string m_ViewId;
    public string viewId
    {
        get { return m_ViewId; }
        set { m_ViewId = value; }
    }

    public bool deletePreView = true;

    /// <summary>
    /// ViewController Manager 
    /// </summary>
    public ViewControllerNav parentNav
    {
        get
        {
            return this.GetComponentInParent<ViewControllerNav>();
        }
    }

    public void RemoveFromNav()
    {
        parentNav.Remove(this);
    }
    public void WillLoadView(System.Action<ViewController> onEnd)
    {
        WillLoadView();

        onEnd?.Invoke(this);
        //Debug.Log("WillLoadDisplay: " + this.name);
    }

    /// <summary>
    /// Call at ViewController begin load . after the ViewControllerLoadAnimation -> PlayShowAnimation
    /// </summary>
    protected virtual void WillLoadView()
    {

        //Debug.Log("WillLoadDisplay: " + this.name);
    }

    /// <param name="initEnd"></param>
    public void OnDidLoadView(System.Action initEnd)
    {
        try
        {
            timer = 0;

            DidLoadView();
        }
        catch (Exception e)
        {
            GameDebug.LogException(e);
        }
        
        if (initEnd != null)
            initEnd();

    }


    /// <summary>
    /// Call after WillLoadView , before the ViewControllerLoadAnimation -> PlayHideAnimation
    /// </summary>
    public virtual void DidLoadView()
    {

    }


    /// <summary>
    /// Call after OnDidLoadView, after the ViewControllerLoadAnimation -> PlayHideAnimation
    /// </summary>
    /// <param name="initEnd"></param>
    public virtual void OnLoadedViewEnd()
    {

    }


    /// <summary>
    ///  Call at ViewController begin hide . after the ViewControllerLoadAnimation -> PlayShowAnimation
    /// </summary>
    public virtual void WillHideView()
    {
        //  Debug.Log("WillHideDisplay: "+this.name);
    }

    public void OnDidHideView(System.Action onEnd)
    {
        try
        {
            DidHideView();
        }
        catch (Exception e)
        {
            GameDebug.LogException(e);
        }


        if (onEnd != null)
            onEnd();
    }

    /// <summary>
    ///  Call after WillHideView, before the ViewControllerLoadAnimation -> PlayHideAnimation
    /// </summary>
    public virtual void DidHideView()
    {

    }

    /// <summary>
    ///  Call after DidHideView, after the ViewControllerLoadAnimation -> PlayHideAnimation
    /// </summary>
    public virtual void OnViewDelete()
    {

    }


    /// <summary>
    /// Delay call onDelete, avoid excetion interrupt the call of 'OnViewDelete'
    /// </summary>
    public bool toDelete {get; set; }

    public float deleteDelayTime = 0.8f;
    float timer = 0;
    protected virtual void Update()
    {
        if (toDelete)
        {
            timer += Time.deltaTime;
            if(timer > deleteDelayTime)
            {
                timer = 0;
                if (toDelete)
                {
                    GameDebug.Log($"{this.name} OnDelete");
                    toDelete = false;
                    OnViewDelete();
                }
            }
        }
    }
}

